/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "processor.h"
#include <mc/log.h>

namespace dev {

bool Processor::read_gpu_partnumber(postbox_ptr postbox) {
    if (!postbox) {
        elog("PartNumber: postbox not initialized");
        return false;
    }
    try {
        SMBusPostboxProtocol::RequestParams params;
        params.name           = "CardGPUPartNumber";
        params.opcode         = 0x5;
        params.arg1           = 0x4;
        params.arg2           = 0x0;
        params.capa_dword_num = 1;
        params.capa_mask      = 0x00000010;
        params.data_out       = true;
        params.data_len       = 16;
        params.data_type      = SMBusPostboxProtocol::KEEP_ASCII;
        params.data_mask      = 0xffffffff;

        std::string result_str = postbox->sendRequest(params);
        if (result_str.empty()) {
            elog("GPU PartNumber: empty response");
            return false;
        }
        elog("GPU PartNumber read: ${GPUPartNumber}", ("GPUPartNumber", result_str));
        try {
            PartNumber.set_value(result_str);
        } catch (const std::exception& e) {
            elog("GPUPartNumber Set value failed: ${error}", ("error", e.what()));
        }
        return true;
    } catch (const std::exception& e) {
        elog("GPUPartNumber: exception: ${e}", ("e", e.what()));
        return false;
    }
}

} // namespace dev

MC_REFLECT(dev::Processor)
